package org.example.Config;

import org.example.common.Result;
import org.example.service.UserProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;
@Component
public class AuthInterceptor implements HandshakeInterceptor {

    @Autowired
    private UserProjectService userProjectService;

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
                                   WebSocketHandler wsHandler, Map<String, Object> attributes) {
        String path = request.getURI().getPath();
        String project = path.substring(path.lastIndexOf('/') + 1);
        String username = request.getURI().getQuery().split("=")[1];
        String s = "通过";

//        List<String> list =userProjectService.selectUserProject2( username, s);
//        if (list!=null) {
        attributes.put("project", project);
        attributes.put("username", username);
        return true;
//        }
//        return false;
//    }
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
                               WebSocketHandler wsHandler, Exception exception) {}
}